from django_redis import get_redis_connection
from rest_framework import serializers


class RegiserSMSCodeSerializer(serializers.Serializer):
    '''
    校验验证码,uuid
    '''
    # required=TRUE字段必须传
    text=serializers.CharField(max_length=4,min_length=4,required=True,label='用户输入的验证码')
    # uuid=serializers.UUIDField(label='验证码唯一性')
    image_code_id=serializers.UUIDField(label='验证码唯一性')
    def validate(self, attrs):
        # 图片验证码失效
        text=attrs.get('text')
        image_code_id = attrs.get('image_code_id' )
        # 图片验证码不一致

        redis_conn=get_redis_connection('code')
        redis_text=redis_conn.get('img_%s'%image_code_id)
        if redis_text is None:
            raise serializers.ValidationError('图片验证码过期')
        if redis_text.decode().lower()!=text.lower():
            raise serializers.ValidationError('输入不一致')
        return attrs
    pass